//
//  EmoticonTextAttachment.swift
//  emoticonKB
//
//  Created by mac on 16/3/27.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class EmoticonTextAttachment: NSTextAttachment {

    //表情图片转换为属性文本
    var chs: String?
    
    //将表情图片转换为属性文本
    class func emoticonImageToimageText(emo: Emoticons,font: UIFont) -> NSAttributedString {
        //附件类型 NSAttributedString
        let attachment = EmoticonTextAttachment()
        //1.将图片添加到附件中
        attachment.image = UIImage(contentsOfFile: emo.imagePath ?? "")
        attachment.chs = emo.chs
        let fontHeight = font.lineHeight ?? 0
        
//frmae ＝ center + bounds * transform  bounds 和滚动视图 contentOffset 一致
        attachment.bounds = CGRect(x: 0, y: -4, width: fontHeight, height: fontHeight)
        
//属性文本可以通过附近实例化 通常都是使用可变的属性文本 可以添加属性
        let imageText = NSMutableAttributedString(attributedString: NSAttributedString(attachment: attachment))
        
        //设置字体大小
        imageText.addAttribute(NSFontAttributeName, value: font, range: NSRange(location: 0,length: 1))
        
        return imageText
    }
    
        
}
